<template>
	<view>
		<my-search @myclick="test"></my-search>
	</view>
	<view class="scroll-view">
		<!-- 左侧 -->
		<scroll-view class="left-scroll-view" scroll-y="true" :style="{height: wh + 'px'}">
			<block v-for="(item,index) in cateList" :key="index">
				<view :class="['left-scroll-view-item', index === active ? 'active' : '']" @click="changeActive(index)">{{item.cat_name}}</view>
			</block>
			

		</scroll-view>
		<!-- 右侧 -->
		<scroll-view class="right-scoal-view" scroll-y="true" :style="{height: wh + 'px'}" :scroll-top="scrolltop">
			<block v-for="(item,index) in cateLevel2" :key="index">
				<view class="cate-lev2">/{{item.cat_name}}/</view>
					<view class="cate-lev3-list" >
						<view class="cate-lev3-item" v-for="(item,index) in cateLevel3" :key="index" @click="toList(item)">
							<image :src="item.cat_icon"></image>
							<text>{{item.cat_name}}</text>
						</view>
					</view>
			</block>
		</scroll-view>
	</view>
</template>

<script>
	import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
		mixins:[badgeMix],
		data() {
			return {
				//左侧分类列表
				cateList:[],
				//右侧二级列表
				cateLevel2:[],
				//右侧三级列表
				cateLevel3:[],
				//窗口可用高度
				wh:0,
				//当前选中
				active:0,
				//滑动条离顶部的距离
				scrolltop:0
				
			};
		},
		onLoad(){
			const sysInfo = uni.getSystemInfoSync()
			
			this.getCateList()
			
			this.wh=sysInfo.windowHeight-50
		},
		
		methods:{
			test(){
				uni.navigateTo({
					url:"/subpackges/search/search"
				})
			},
			async getCateList(){
				const {data:res} = await uni.$http.get('/api/public/v1/categories')
				if(res.meta.status!=200) return uni.$showMsg()
				this.cateList=res.message
				this.cateLevel2=res.message[0].children
				this.cateLevel3=this.cateLevel2[0].children
			},
			changeActive(index){
				this.active=index
				// 重新请求对应数据 ps:将0改为this.active
				// this.getCateList()
				//在一次请求的基础上重新赋值
				this.cateLevel2 = this.cateList[index].children
				this.cateLevel3 = this.cateLevel2[index].children
				this.scrolltop = this.scrolltop === 0 ? 1 : 0
			},
			toList(item){
				uni.navigateTo({
					url:'/subpackges/list/list?cid='+item.cat_id
				})
			}
		}
	}
</script>

<style lang="scss">
.scroll-view{
	display: flex;
	
	.left-scroll-view{
		width: 240rpx;
		
		.left-scroll-view-item{
			background-color: #F7F7F7;
			line-height: 60px;
			text-align: center;
			font-size: 12px;
	
			&.active{
				background-color: #FFFFFF;
				position: relative;
				
				&::before{
					content:'';
					display: block;
					width: 3px;
					height: 30px;
					background-color: #C00000;
					position: absolute;
					top: 25%;
				}
			}
		}
	}
	.cate-lev2{
		font-size: 15px;
		font-weight: bold;
		text-align: center;
		padding: 15px 0;
	}
	.cate-lev3-list{
		display: flex;
		flex-wrap: wrap;
		
		
		.cate-lev3-item{
			margin-bottom: 10px;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			width: 33.33%;
			image{
				width: 60px;
				height: 60px;
			}
			text{
				font-size: 12px;
			}
		}
		
	}
}


</style>
